package main.Primavera;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;

public class PMConfig extends JDialog {

    private JPanel panel;
    public Dimension screensize;

    private JTextField DBname;
    private JTextField DBpath;
    private JTextField DBport;
    private JLabel DBnametext;
    private JLabel DBpathtext;
    private JLabel DBporttext;
    private JButton saveDB;
    private JButton cancel;
    private String[] servlist;
    public DefaultListModel listModel;
    public JList jList;
    public DefaultComboBoxModel serverBoxModel;
    private boolean flag = false;
    private String old_server_name;
    private int choose_server;

    public PMConfig(DefaultComboBoxModel serverBox, DefaultListModel listModel, JList list) {
        this.listModel = listModel;
        this.jList = list;
        this.serverBoxModel = serverBox;
        setModal(true);
    }

    public void Openconfig(String server_name, int choose_server) {
        old_server_name = server_name;
        this.choose_server = choose_server;
        panel = new JPanel();
        setSize(new Dimension(300,200));
        screensize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(screensize.width / 2 - 150, screensize.height / 2 - 100);

        GridBagConstraints c = new GridBagConstraints();
        panel.setLayout(new GridBagLayout());

        DBname = new JTextField(15);
        DBnametext = new JLabel("Имя БД:");
        DBpath = new JTextField(15);
        DBpathtext = new JLabel("Путь БД:");
        DBport = new JTextField(15);
        DBporttext = new JLabel("Порт БД");

        if (!server_name.equals("")) {
            flag = true;
            ConfigXML configXML = new ConfigXML();
            servlist = configXML.getChooseConfig(server_name);

            DBname.setText(servlist[0]);  //Имя сервера
            DBpath.setText(servlist[1]);  //Путь сервера
            DBport.setText(servlist[2]);  //Порт сервера
        }

        //Имя БД

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.weightx = 1;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.insets = new Insets(30,10,0,10);
        panel.add(DBnametext,c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 1;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.weightx = 1;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.insets = new Insets(0,10,10,10);
        c.fill = GridBagConstraints.BOTH;
        panel.add(DBname,c);

        //Путь БД

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 2;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.weightx = 1;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.insets = new Insets(10,10,0,10);
        panel.add(DBpathtext,c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 3;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.weightx = 1;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.insets = new Insets(0,10,10,10);
        c.fill = GridBagConstraints.BOTH;
        panel.add(DBpath,c);

        //Порт БД

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 4;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.weightx = 1;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.insets = new Insets(10,10,0,10);
        panel.add(DBporttext,c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 5;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.weightx = 1;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.insets = new Insets(0,10,30,10);
        c.fill = GridBagConstraints.BOTH;
        panel.add(DBport,c);

        saveDB = new JButton();
        saveDB.setText("Сохранить");
        saveDB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                buttonSave();
            }
        });

        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 1;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.weightx = 0;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.insets = new Insets(0,10,0,10);
        panel.add(saveDB,c);

        cancel = new JButton();
        cancel.setText("Отмена");
        cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                setVisible(false);
            }
        });

        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 3;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.weightx = 0;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.insets = new Insets(0,10,0,10);
        panel.add(cancel,c);

        add(panel);
        setVisible(true);
    }

    public void buttonSave() {
        ConfigXML configXML = new ConfigXML();
        if (flag == false) {
            configXML.addConfig(DBname.getText(), DBpath.getText(), Integer.parseInt(DBport.getText()));
            listModel.addElement(DBname.getText());
            serverBoxModel.addElement(DBname.getText());
        } else {
            configXML.editConfig(old_server_name, DBname.getText(), DBpath.getText(), Integer.parseInt(DBport.getText()));
            listModel.setElementAt(DBname.getText(), choose_server);
            serverBoxModel.removeElementAt(choose_server);
            serverBoxModel.insertElementAt(DBname.getText(), choose_server);
            jList.validate();
        }
        setVisible(false);
    }
}